// pages/user/user.js
import { getworth, getarea,addAddress,delAddres,modifyAddress,getAddres } from '../../api/article'
import { getId } from '../../api/user'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show1:false,
    list1:[],
    parmas:{
        addressId:'',
        name:'',
        phone:'',
        province:'',
        city:'',
        area:'',
        address:''
    },
    level:1,
    code:1,
    provinceCode:1,
    cityCode:null,
    areaCode:null,
    addrs:[]
  },
  onClose(){
    this.setData({
        show1:false
    })
  },
  onChange(e){
    this.setData({
        'parmas.name':e.detail
    })
  },
  onChange2(e){
    this.setData({
        'parmas.phone':e.detail
    })
  },
  onChange3(e){
    this.setData({
        'parmas.address':e.detail
    })
  },
  async handleOpen1(e){
      let code = null
      if(e.currentTarget.dataset.level==1){
        code = 1
        this.setData({
            'parmas.city':'',
            'parmas.area':'',
        })
      }else if(e.currentTarget.dataset.level==2){
        code = this.data.provinceCode
        this.setData({
            'parmas.area':'',
        })
      }else if(e.currentTarget.dataset.level==3){
        code = this.data.cityCode
      }
    const res = await getarea({pcode:code})
    this.setData({
        level:e.currentTarget.dataset.level,
        show1:true,
        list1:res.content
    })
  },
  handleCilck(e){
    if(this.data.level==1){
        this.setData({
            'parmas.province':e.currentTarget.dataset.item.name,
            provinceCode:e.currentTarget.dataset.item.code
        })
    }else if(this.data.level==2){
        this.setData({
            'parmas.city':e.currentTarget.dataset.item.name,
            cityCode:e.currentTarget.dataset.item.code
        })
    }else{
        this.setData({
            'parmas.area':e.currentTarget.dataset.item.name,
            areaCode:e.currentTarget.dataset.item.code
        })
    }
    this.setData({
        show1:false,
        code:e.currentTarget.dataset.item.code
    })
  },
  async handleAdd(){
    const res = await getId({type:10})
    this.setData({
        'parmas.addressId':res.content
    })
    const reslut = await addAddress(this.data.parmas)
  
        this.init()
  },
  async init(){
    const res = await getAddres({})
    this.setData({
        addrs:res.content
    })
 
  },
  async handleDelAddr(e){
    console.log(wx.getStorageSync('user').userId)
    const res = await delAddres({userId:wx.getStorageSync('user').userId,addressId:e.currentTarget.dataset.id})
    this.init()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.init()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})